package JDBCDemo;

import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;


/**
 * API:Statement
 */

public class Demo03_Statement {

    public static void main(String[] args) throws Exception {
        testDDL();
    }

    //执行DML语句
    @Test
    public static void testDML() throws  Exception {
        //1.注册驱动
        //Class.forName("com.mysql.cj.jdbc.Driver");

        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/mytest";
        String username = "root";
        String password = "123456";
        Connection con = DriverManager.getConnection(url,username,password);

        //3.定义SQL语句
        String sql = "update account set money = 10000 where name = '张三'";

        //4.获取执行sql的对象 Statement
        Statement stmt = con.createStatement();

        //5.执行sql
        int count = stmt.executeUpdate(sql);
        //返回影响函数
        //System.out.println(count);
        //6. 处理结果
        if(count > 0){
            System.out.println("修改成功~");
        }else{
            System.out.println("修改失败~");
        }

        //释放资源
        stmt.close();
        con.close();
    }

    //执行DDL语句
    @Test
    public static void testDDL() throws  Exception {
        //1. 注册驱动
        //Class.forName("com.mysql.jdbc.Driver");
        //2. 获取连接：如果连接的是本机mysql并且端口是默认的 3306 可以简化书写
        String url = "jdbc:mysql:///mytest?useSSL=false";
        String username = "root";
        String password = "123456";
        Connection con = DriverManager.getConnection(url, username, password);
        //3. 定义sql
        String sql = "drop table emp1";
        //4. 获取执行sql的对象 Statement
        Statement stmt = con.createStatement();
        //5. 执行sql
        int count = stmt.executeUpdate(sql);
        //执行完DDL语句，可能是0
        //6. 处理结果
        System.out.println(count);

        //7. 释放资源
        stmt.close();
        con.close();
    }


}
